package cn.hs.service.impl;

import cn.hs.entity.Article;
import cn.hs.entity.Articletype;
import cn.hs.entity.Consult;
import cn.hs.mapper.ArticleMapper;
import cn.hs.mapper.ArticletypeMapper;
import cn.hs.mapper.ConsultMapper;
import cn.hs.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @ClassName ArticleServiceImpl
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月22日 20:57
 * @Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticletypeMapper articletypeMapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Override
    public PageInfo findAll(Integer pageNum, Integer pageSize,Article article) {
        //分页业务实现
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.selectAll(article);
        //遍历文章集合
        for(Article article1:articles){
            Integer typeId = article1.getTypeId();
            Articletype articletype = articletypeMapper.selectByPrimaryKey((long) typeId);
            article1.setArticletype(articletype);
            Integer teacherId = article1.getTeacherId();
            Consult consult = consultMapper.selectByPrimaryKey((long) teacherId);
            article1.setConsult(consult);
        }
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public int insert(Article article) {
        return articleMapper.insertAll(article);
    }

    @Override
    public int update(Article article) {
        return articleMapper.updateSelective(article);
    }

    @Override
    public int delete(int id) {
        return articleMapper.deleteById(id);
    }
}
